package com.woniu.config;



import com.woniu.interceptor.LoginInterceptor;
import com.woniu.interceptor.PermissionInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author: Gwj
 * @Date: 2024/6/25 9:48
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    public void addCrosMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //可以跨域的域名
                .allowedOriginPatterns("*")
                //是否支持发送cookie
                .allowCredentials(true)
                //放行的请求方式
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                //放行的头部信息
                .allowedHeaders("*")
                //暴露的头部信息
                .exposedHeaders("*")
                .maxAge(3600);
    }

    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    @Bean
    public PermissionInterceptor permissionInterceptor() {
        return new PermissionInterceptor();
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] path = {
                "/account/login",
                "/index",
                "/error",
                "/doc.html",
                "/webjars/**",
                "/swagger-resources",
                "/favicon.ico",
                "/swagger-resources/**",
                "/file/**",
                "/code/**",
        };
        registry.addInterceptor(this.loginInterceptor()).addPathPatterns("/**").excludePathPatterns(path);

        registry.addInterceptor(this.permissionInterceptor()).addPathPatterns("/**").excludePathPatterns(path);
    }
}
